package org.kou.servlet;

import java.util.List;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.kou.bean.ShoppingBean;
import org.sen.db.DBProvider;
import org.sen.utils.PublicUtils;

public class ShoppingCar {
	public List<ShoppingBean> queryLoginInfo(ShoppingBean admink) {
		// 调用db层进行数据库操作
		// 登录查询语句
		String sql = "select * from shoppingcar where userid = ?";
		// 调用db层进行数据库查询操作
		List<ShoppingBean> lstAdmins = PublicUtils.getInstance(DBProvider.class).query(sql,
				new BeanListHandler<>(ShoppingBean.class), admink.getUserid());
		// 返回结果
		return lstAdmins;
	}

	public int savedata(ShoppingBean admink) {
		// 默认返回值
		int i = 0;
		// 新增和修改的区别在于：参数实体类的主键字段是否为null，为null则是新增操作，不为null则是修改操作
		// 新增的sql语句
		if (admink != null) {
			// 新增操作
			if (admink.getUserid() == null) {
				// 新增sql语句
				String strSql = "insert into shoppingcar(userid,phoneid) values(?,?)";
				// 调用db层进行数据库操作
				i = PublicUtils.getInstance(DBProvider.class).update(strSql, admink.getUserid(),admink.getPhoneid());
			} else {
				// 修改(回复)操作
				// 修改sql语句
				String strSql = "update shoppingcar set phoneid = ? WHERE userid = ?";
				// 调用db层进行数据库操作
				i = PublicUtils.getInstance(DBProvider.class).update(strSql, admink.getPhoneid(), admink.getUserid());
			}
		}
		return i;
	}

	public int savedelete(ShoppingBean admink) {
		// 默认返回值
		int i = 0;
		if (admink != null) {
			// 删除操作
			if (admink.getUserid() != null) {
				// 删除sql语句
				String strSql = "delete from shoppingcar where userid = ?;";
				// 调用db层进行数据库操作
				i = PublicUtils.getInstance(DBProvider.class).update(strSql, admink.getUserid());
			}
		}
		return i;
	}
}